package sr.myserver.controller;

import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sr.myserver.commons.Constants;
import sr.myserver.config.JwtConfig;
import sr.myserver.entity.SysMenus;
import sr.myserver.service.SysMenusService;
import sr.myserver.vo.Menus;
import sr.myserver.vo.SelectData;
import sr.myserver.vo.common.Basic;
import sr.myserver.vo.common.PageRequest;
import sr.myserver.vo.common.PageResponse;

import java.util.List;

/**
 * 菜单信息
 */
@Slf4j
@RestController
@RequestMapping("/sysmenus")
@Api(tags = "菜单信息")
public class SysMenusController {

    @Autowired
    SysMenusService sysMenusService;

    /**
     * 指定ID获取数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "指定ID获取数据")
    public SysMenus get(@PathVariable("id") String id) {
        return sysMenusService.get(id);
    }

    /**
     * 添加数据
     *
     * @param entity
     * @return
     */
    @PostMapping
    @Operation(summary = "添加数据")
    public Basic.Response post(@RequestBody SysMenus entity) {
        return sysMenusService.add(entity);
    }

    /**
     * 指定ID修改数据
     *
     * @param entity
     * @return
     */
    @PutMapping("/edit")
    @Operation(summary = "指定ID修改数据")
    public Basic.Response put(@RequestBody SysMenus entity) {
        return sysMenusService.update(entity);
    }

    /**
     * 指定ID删除数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "指定ID删除数据")
    public Basic.Response delete(@PathVariable("id") String id) {
        return sysMenusService.deleteById(id);
    }

    /**
     * 分页列表数据
     *
     * @param request
     * @return
     */
    @PutMapping
    @Operation(summary = "分页列表数据")
    public PageResponse<SysMenus> queryTables(@RequestBody PageRequest request) {
        return sysMenusService.queryTables(request);
    }

    /**
     * 分页下拉数据
     *
     * @param request
     * @return
     */
    @PostMapping("/select")
    @Operation(summary = "分页下拉数据")
    public PageResponse<SelectData> querySelect(@RequestBody PageRequest request) {
        return sysMenusService.querySelect(request);
    }

    /**
     * 功能管理-查询菜单下支持的按钮下拉
     *
     * @param request
     * @return
     */
    @PostMapping("/menuselect")
    @Operation(summary = "功能管理-查询菜单下支持的按钮下拉")
    public PageResponse<SelectData> menuSelect(@RequestBody PageRequest request) {
        return sysMenusService.menuSelect(request);
    }

    /**
     * 查询主页菜单
     *
     * @param token
     * @return
     */
    @PostMapping("/home")
    @Operation(summary = "查询主页菜单")
    public List<Menus> home(@CookieValue(Constants.AUTHORIZATION) String token) {
        return sysMenusService.home(JwtConfig.thinning(token));
    }

    /**
     * 加载权限管理表单菜单
     *
     * @param token
     * @return
     */
    @PostMapping("/roles")
    @Operation(summary = "加载权限管理表单菜单")
    public List<Menus> roles(@CookieValue(Constants.AUTHORIZATION) String token) {
        return sysMenusService.roles(JwtConfig.getUserId(token));
    }
}

